<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;

/**
 * 服务类型控制器
 *
 * @author derek <693472824@qq.com>
 */
class ClassController extends BaseController {

    /**
     * 
     */
    public function index(){
        
        $tree = D('Class', 'Logic')->getClassTree();
        $this->assign('__TREE__', $tree);
        $this->display();
    
    }

    /**
     * 增加类型以及修改类型的主界面
     *
     * @param integer $dict_id 类型ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
        
        $classID = I('class_id');
        $classLogic = D('Class', 'Logic');
        
        $classTree = $classLogic->getClassTree();
        
        if (!empty($classID)) {
            
            $classModel = $classLogic->getClassByID($classID);
            
            if (empty($classModel)) {
                $this->error('获取数据失败', U('index'));
            } else {
                $this->assign('classModel', $classModel);
            }
        }
        
        $this->assign('classTree', $classTree);
        $this->display();
    }


    /**
     * 保存服务类型信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
    
        $classLogic = D('Class', 'Logic');
        $result = $classLogic->saveAction();
    
        if ($result === false) {
            $this->error('操作失败' . $classLogic->getError());
        } else {
            $this->success('操作成功', U('index'));
        }
    }
    
    /**
     * 类型删除
     * @author derek <693472824@qq.com>
     */
    public function del(){
        
        $classID = I('class_id');
        
        if (empty($classID)) {
            $this->error('参数错误');
        }
        
        $classLogic = D('Class', 'Logic');
        $result = $classLogic->del($classID);
        
        if ($result === false) {
            $this->error('操作失败', U('index'));
        } else {
            $this->success('操作成功！', U('index'));
        }
    }
    
    /**
     * 服务类型细分项
     */
    public function detailIndex(){
        
        $classID = I('class_id');
        
        if (empty($classID)) {
            $this->error('参数错误');
        }
        
        $map['class_id'] = $classID;
        $name = I('detail_name');
        
        if(!empty($name)){
            $map['detail_name'] = array('like', '%'.$name.'%');
        }
        
        $detailModel = M('ClassDetail');
        $list = $this->lists($detailModel, $map);
        
        $this->assign('detail_name', $name);
        $this->assign('class_id', $classID);
        $this->assign('list', $list);
        $this->display();
    }
    
    /**
     * 增加细分项
     * @author derek <693472824@qq.com>
     */
    public function editDetailAction() {
    
        $detailID = I('detail_id');
        $classID = I('class_id');
        $classLogic = D('Class', 'Logic');
        $classModel = $classLogic->getClassByID($classID);
    
        if (!empty($detailID)) {
    
            $detailModel = $classLogic->getDetailByID($detailID);
    
            if (empty($detailModel)) {
                $this->error('获取数据失败', U('index'));
            } else {
                $this->assign('detailModel', $detailModel);
            }
        }
    
        $this->assign('classModel', $classModel);
        $this->display();
    }

    /**
     * 保存细分项信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveDetailAction() {
    
        $classID = I('class_id');
        
        $classLogic = D('Class', 'Logic');
        $result = $classLogic->saveDetailAction();
    
        if ($result === false) {
            $this->error('操作失败' . $classLogic->getError());
        } else {
            $this->success('操作成功', U('detailIndex', 'class_id='.$classID));
        }
    }
    
    /**
     * 类型删除
     * @author derek <693472824@qq.com>
     */
    public function delDetail(){
    
        $detailID = I('detail_id');
        $classID = I('class_id');
    
        if (empty($detailID)) {
            $this->error('参数错误');
        }
    
        $classLogic = D('Class', 'Logic');
        $result = $classLogic->delDetail($detailID);
    
        if ($result === false) {
            $this->error('操作失败', U('detailIndex', 'class_id='.$classID));
        } else if($result == '-1') {
            $this->error('操作失败，已生成服务订单，不可删除', U('detailIndex', 'class_id='.$classID));
        }else{
            $this->success('操作成功！', U('detailIndex', 'class_id='.$classID));
        }
    }
}